﻿using MTQ.Common;
using MTQ.IBLL;
using MTQ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTQ.BLL
{
    public partial class Base_UserInfoService : BaseService<Base_UserInfo>, IBase_UserInfoService
    {

        #region ---有系统自动生成---
        //只要想操作数据库，我们只要拿到DbSession就行
        //public override void SetCurrentRepository()
        //{
        //    CurrentRepository = _DbSession.UserInfoRepository;
        //} 
        #endregion

        #region ------注释掉的东西-------
        //访问DAL实现CRUD

        //private DAL.UserInfoRepository _userInfoRepository = new UserInfoRepository();

        //依赖接口编程
        //private IUserInfoRepository _userInfoRepository = new UserInfoRepository();

        //private IUserInfoRepository _userInfoRepository = RepositoryFactory.UserInfoRepository;

        //public  UserInfo AddUserInfo(UserInfo userInfo)
        //{
        //    return _userInfoRepository.AddEntity(userInfo);
        //}

        //public bool UpdateUserInfo(UserInfo userInfo)
        //{
        //    return _userInfoRepository.UpdateEntity(userInfo);
        //} 
        #endregion

        /// <summary>
        /// 完成了对用户的校验
        /// </summary>
        /// <param name="userInfo">传递了用户类</param>
        /// <returns>返回了执行结果的枚举类型</returns>
        public LoginResult CheckUserInfo(Base_UserInfo userInfo)
        {
            //首先判断用户名，密码是否为空
            if (string.IsNullOrEmpty(userInfo.User_UserName))
            {
                return LoginResult.UserIsNull;
            }
            if (string.IsNullOrEmpty(userInfo.User_Password))
            {
                return LoginResult.PwdIsNUll;
            }
            //如果不为空的话则去数据库中查询信息


            //在这里会去数据库检查是否有数据，如果没有的话就会返回一个空值
            var LoginUserInfoCheck = _DbSession.Base_UserInfoRepository.LoadEntities(x => x.User_UserName == userInfo.User_UserName && x.User_Password == userInfo.User_Password).SingleOrDefault();

            //对返回的结果进行判断
            if (LoginUserInfoCheck == null)
            {
                return LoginResult.UserNotExist;
            }
            if (LoginUserInfoCheck.User_Password != userInfo.User_Password)
            {
                return LoginResult.PwdError;
            }
            if (LoginUserInfoCheck.User_Available == "N")
            {
                return LoginResult.UserActivation;
            }
            else
            {
                userInfo.User_RealName = LoginUserInfoCheck.User_RealName;
                return LoginResult.OK;
            }
        }

        /// <summary>
        /// 判断用户名不能重复
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <returns>返回执行结果的枚举类型</returns>
        public LoginResult CheckUserNameTest(string UserName)
        {
            //首先判断是否为空
            if (String.IsNullOrEmpty(UserName))
            {
                return LoginResult.UserIsNull;
            }
            var checkUserName = _DbSession.Base_UserInfoRepository.LoadEntities(u => u.User_UserName == UserName).FirstOrDefault();
            if (checkUserName != null)
            {
                return LoginResult.UserExist;
            }
            else
            {
                return LoginResult.OK;
            }
        }

        /// <summary>
        /// 实现批量删除数据
        /// </summary>
        /// <param name="deleteIds">批量删除的List集合的参数</param>
        /// <returns>返回受影响的行数</returns>
        public int DeleteUsers(List<Guid> deleteIds)
        {
            foreach (var deleteID in deleteIds)
            {
                _DbSession.Base_UserInfoRepository.DeleteEntity(new Base_UserInfo()
                {
                    User_Id = deleteID
                });
            }
            return _DbSession.SaveChanges();
        }

        ///// <summary>
        ///// 实现对多条件查询的判断方法的封装
        ///// </summary>
        ///// <param name="query">引用传递，传递参数的信息</param>
        ///// <returns>返回用户类的IQueryable集合</returns>
        //public IQueryable<Base_UserInfo> LoadSearchData(Base_UserInfoQuery query)
        //{
        //    var temp = _DbSession.Base_UserInfoRepository.LoadEntities(u => true);
        //    //首先过滤姓名
        //    if (!string.IsNullOrEmpty(query.RealName))
        //    {
        //        temp = temp.Where<Base_UserInfo>(u => u.RealName.Contains(query.RealName));  //like '%mmm%'
        //    }
        //    if (!string.IsNullOrEmpty(query.Telephone))
        //    {
        //        temp = temp.Where<Base_UserInfo>(u => u.Telephone.Contains(query.Telephone));
        //    }
        //    if (!string.IsNullOrEmpty(query.EMail))
        //    {
        //        temp = temp.Where<Base_UserInfo>(u => u.Email.Contains(query.EMail));
        //    }
        //    if (query.Enabled != -1)
        //    {
        //        temp = temp.Where<Base_UserInfo>(u => u.Enabled == query.Enabled);
        //    }
        //    if (!string.IsNullOrEmpty(query.AuditStatus) && query.AuditStatus != "-1")
        //    {
        //        temp = temp.Where<Base_UserInfo>(u => u.AuditStatus.Contains(query.AuditStatus));
        //    }
        //    temp = query.DeletionStateCod == 1 ? temp.Where<Base_UserInfo>(u => u.DeletionStateCode == query.DeletionStateCod) : temp.Where<Base_UserInfo>(u => u.DeletionStateCode == 0);
        //    query.Total = temp.Count();
        //    return temp.OrderBy(u => u.SortCode).Skip(query.PageSize * (query.PageIndex - 1)).Take(query.PageSize);
        //}

        /// <summary>
        /// 执行对用户设置角色的封装
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="roleIDs">角色集合的ID</param>
        /// <param name="userInfo">传递过去用户登录的session</param>
        /// <returns>返回是否执行成功的标志</returns>
        public bool SetBaseUserRole(Guid userID, List<Guid> roleIDs, Base_UserInfo userInfo)
        {
            //首先根据传递过来的userID判断用户是否存在
            var currentUserInfo = _DbSession.Base_UserInfoRepository.LoadEntities(c => c.User_Id == userID).FirstOrDefault();
            if (currentUserInfo == null)
            {
                return false;
            }
            ////首先获取到角色表中的所有信息返回
            //var listRoles = currentUserInfo.R_UserInfo_Role.ToList();
            //foreach (var t in listRoles)
            //{
            //    _DbSession.R_User_RoleRepository.DeleteEntity(t);
            //}
            ////真正的删除了这个用户下面的所有的数据
            //_DbSession.SaveChanges();
            ////然后重新给这个用户赋予权限
            //foreach (var roleID in roleIDs)
            //{
            //    //给用户批量插入角色，在中间表中,这里需要改成一个批量提交添加数据的
            //    var rUserInfoRole = new R_User_Role
            //    {
            //        RoleID = roleID,
            //        UserID = userID,
            //        CreateOn = DateTime.Parse(DateTime.Now.ToString())
            //    };
            //    var user = userInfo;
            //    rUserInfoRole.CreateUserID = user.Code;
            //    rUserInfoRole.CreateBy = user.UserName;
            //    _DbSession.R_User_RoleRepository.AddEntity(rUserInfoRole);
            //}
            //执行真正的添加
            _DbSession.SaveChanges();
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public static List<Base_Button> GetUserButton(Base_UserInfo userInfo)
        {
            List<Base_Button> listButton = new List<Base_Button>();
            if (userInfo.IsSuperAdmin)
            {
                listButton = new Base_ButtonService().LoadEntities(x => true).ToList();
            }
            else 
            {
                Base_UserRoleService urs = new Base_UserRoleService();
                List<Guid> listRoles = urs.LoadEntities(x => x.Base_UserId == userInfo.User_Id).Select(x => x.Base_RoleId).ToList();
                if (listRoles !=null && listRoles.Count>0)
                {
                    try
                    {
                        //Guid roleid =listRoles.First();
                        List<Guid> listbuttonid = new List<Guid>();

                        listbuttonid = new Base_RoleButtonService().LoadEntities(x => listRoles.Contains( x.Role_Id)).Select(x => x.Button_Id).ToList();
                        listButton = new Base_ButtonService().LoadEntities(x => listbuttonid.Contains(x.Button_Id)).ToList();

                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
            return listButton;
        }
    }
}
